//
//  TTNAIManager.m
//  Tint
//
//  Created by Kevin Wu on 2/23/12.
//  Copyright (c) 2012 Telligenty. All rights reserved.
//

#import "TTNAIManager.h"


@implementation TTNAIManager

TTSynthesizeSingleton(TTNAIManager);


#pragma mark - Memory

- (id)init {
    self = [super init];
    if (self) {
        _networkUsers = [[NSMutableArray weakArray] retain];
    }
    return self;
}

- (void)dealloc {
    TTRelease(_networkUsers);
    [super dealloc];
}



#pragma mark - Public

- (void)addNetworkUser:(id)user {
    @synchronized (self) {
        [_networkUsers addNonIdenticalObjectIfNotNil:user];
        [UIApplication sharedApplication].networkActivityIndicatorVisible = TTIsArrayWithItems(_networkUsers);
    }
}

- (void)removeNetworkUser:(id)user {
    @synchronized (self) {
        [_networkUsers removeObjectIdenticalTo:user];
        [UIApplication sharedApplication].networkActivityIndicatorVisible = TTIsArrayWithItems(_networkUsers);
    }
}

- (void)removeAllNetworkUsers {
    @synchronized (self) {
        [_networkUsers removeAllObjects];
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    }
}

- (BOOL)isNetworkUser:(id)user {
    return ([_networkUsers indexOfObjectIdenticalTo:user] != NSNotFound);
}

@end
